<template>
  <el-form :inline="true" :model="modelValue" class="search-form">
    <slot></slot>
    <el-form-item>
      <el-button type="primary" :icon="Search" @click="handleSearch">
        搜索
      </el-button>
      <el-button :icon="Refresh" @click="handleReset">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { Search, Refresh } from "@element-plus/icons-vue";

const props = defineProps<{
  modelValue: Record<string, any>;
}>();

const emit = defineEmits<{
  (e: "update:modelValue", value: Record<string, any>): void;
  (e: "search"): void;
  (e: "reset"): void;
}>();

const handleSearch = () => {
  emit("search");
};

const handleReset = () => {
  // 重置表单
  const resetValue = Object.keys(props.modelValue).reduce((acc, key) => {
    acc[key] = "";
    return acc;
  }, {} as Record<string, any>);

  emit("update:modelValue", resetValue);
  emit("reset");
};
</script>

<style lang="scss" scoped>
.search-form {
  margin-bottom: 20px;

  @media screen and (max-width: 768px) {
    :deep(.el-form-item) {
      margin-bottom: 10px;
    }
  }
}
</style>
